const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//转换时间格式 （年月日）
function setTime(timestr){
  var date = new Date(timestr);
  var Y = date.getFullYear();
  var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
  var D = date.getDate();
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return Y+'年'+M+'月'+D+'日';
}

//转换时间格式 （时分）
function setTime2(mss){
  var days = parseInt(mss / (1000 * 60 * 60 * 24));
  var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = (mss % (1000 * 60)) / 1000;
  return hours + " : " + minutes;
}


function setTime3(time){
  let timearr = time.replace(" ", ":").replace(/\:/g, "-").split("-");
  let timestr = timearr[3] + " : " + timearr[4]
  return timestr
}

//转换时间格式 （月日）
function setTime4(timestamp) {
  var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + '-';
  // var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  var D = change(date.getDate()) + ' ';
  var h = change(date.getHours()) + ':';
  var m = change(date.getMinutes()) + ':';
  var s = change(date.getSeconds());
  return  M + '.' + D;
}

function change(t) {
  if (t < 10) {
      return "0" + t;
  } else {
      return t;
  }
}

//转换时间格式 （时分秒）
function setTime5(mss){
  var days = parseInt(mss / (1000 * 60 * 60 * 24));
  var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = parseInt((mss % (1000 * 60)) / 1000);
  return hours + ":" + minutes + ":" + seconds;
}

//转换时间格式 （月日）
function setTime6(str){
  str = str.replace(/[^\d]/g,'/');
  str= (str.substring(str.length-1) == '/')? str.substring(0,str.length-1) : str ;
  var date = new Date(str);
  var Y = date.getFullYear();
  var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
  var D = date.getDate();
  return M +'.' + D
}

//转换时间格式 （年月日）
function setTime7(timestr){
  var date = new Date(timestr);
  var Y = date.getFullYear();
  var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
  var D = date.getDate();
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return Y+'-'+M+'-'+D;
}

//转换时间格式 （年月日）
function setTime8(timestr){
  var date = new Date(timestr * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear();
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  var D = change(date.getDate());
  var h = change(date.getHours()) + ':';
  var m = change(date.getMinutes()) + ':';
  var s = change(date.getSeconds());
  return Y + '/' + M + '/' + D;
}

//转换时间格式 （年月日）
function setTime9(timestr){
  var date = new Date(timestr * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear();
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  var D = change(date.getDate());
  var h = change(date.getHours()) + ':';
  var m = change(date.getMinutes()) + ':';
  var s = change(date.getSeconds());
  return Y + '-' + M + '-' + D;
}

//转换时间格式 （年月日）
function setTime10(timestr){
  var date = new Date(timestr);
  var Y = date.getFullYear();
  var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
  var D = date.getDate();
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return Y + '-' + M;
}

//转换时间格式 （年月日）
function setTime11(timestr){
  var date = new Date(timestr);
  var Y = date.getFullYear();
  var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
  var D = date.getDate();
  var h = date.getHours();
  h = h < 10 ? ('0' + h) : h;
  var minute = date.getMinutes();
  var second = date.getSeconds();
  minute = minute < 10 ? ('0' + minute) : minute;
  second = second < 10 ? ('0' + second) : second;
  return Y+'-'+M+'-'+D + ' ' + h +':' + minute;
}

//转换时间格式 （年月日）
function setTime12(timestr){
  var date = new Date(timestr * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear();
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  var D = change(date.getDate());
  var h = change(date.getHours()) + ':';
  var m = change(date.getMinutes()) + ':';
  var s = change(date.getSeconds());
  return Y + '-' + M + '-' + D+ ' ' + h + ':' + m;
}

//正则验证
function validation (data,type) {
  let str = ''
  if(type == 'phone')
  {
    str = /^1\d{10}$/
  }
  else if(type == 'idcard')
  {
    str = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  }
  else if(type == 'name')
  {
    str = /^[\u4E00-\u9FA5A-Za-z]+$/
  }
  else if(type == 'number')
  {
    // 验证由数字和26个英文字母组成的字符串
    str = /^[A-Za-z0-9]+$/
  }
  if (str.test(data)) {
    return true
  } 
  else{
    return false
  }
}

//判断是否为特殊字符或者表情
// function judgeSpecialCharacters(nickName){
//   reg = /[~#^$@%&!?%*]/gi;
//   // reg.test(nickName.trim())
//   if (isEmojiCharacter(nickName.trim())) {
//     return true;
//   } 
// }

//判断是否是表情
function isEmojiCharacter(substring) {
  for (var i = 0; i < substring.length; i++) {
    var hs = substring.charCodeAt(i);
    if (0xd800 <= hs && hs <= 0xdbff) {
      if (substring.length > 1) {
        var ls = substring.charCodeAt(i + 1);
        var uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
        if (0x1d000 <= uc && uc <= 0x1f77f) {
          return true;
        }
        }
    } else if (substring.length > 1) {
      var ls = substring.charCodeAt(i + 1);
      if (ls == 0x20e3) {
        return true;
      }
    } else {
      if (0x2100 <= hs && hs <= 0x27ff) {
        return true;
      } else if (0x2B05 <= hs && hs <= 0x2b07) {
        return true;
      } else if (0x2934 <= hs && hs <= 0x2935) {
        return true;
      } else if (0x3297 <= hs && hs <= 0x3299) {
        return true;
      } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
        return true;
      }
    }
  }
} 

function setUnit2(name){
  switch (name) {
    case 'BMI':
      return '%'
    case '体脂率':
      return '%'
    case '体蛋白质':
      return 'kg'
    case '基础代谢':
      return 'kg'
    case '体肌肉量':
      return 'kg'
    case '去脂体重':
      return 'kg'
    case '无机盐':
      return 'kg'
    case '骨矿含量':
      return 'kg'
    default:
      return 'kg'
  }
}
//分钟转换小时
function ChangeHourMinutestr(str) {
  if (str !== "0" && str !== "" && str !== null) {
    return ((Math.floor(str / 60)).toString().length < 2 ? "0" + (Math.floor(str / 60)).toString() : 
    (Math.floor(str / 60)).toString()) + ":" + ((str % 60).toString().length < 2 ? "0" + (str % 60).toString() : (str % 60).toString());
  }
  else
  {
    return "00:00";
  }
}

function showModal(title,content){
  wx.showModal({
    title: title,
    content: content,
    showCancel:false,
    success (res) {
    }
  })
  return
}

module.exports = {
  formatTime: formatTime,
  setTime: setTime,
  setTime2: setTime2,
  setTime3: setTime3,
  setTime4: setTime4,
  setTime5: setTime5,
  setTime6: setTime6,
  setTime7: setTime7,
  setTime8: setTime8,
  setTime9: setTime9,
  setTime10: setTime10,
  setTime11: setTime11,
  setTime12: setTime12,
  validation: validation,
  isEmojiCharacter: isEmojiCharacter,
  setUnit2 : setUnit2,
  ChangeHourMinutestr: ChangeHourMinutestr,
  showModal: showModal
}